home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / hard / hack / i2clib40.lha / i2clib40 / src / Dial.c next >
C/C++ Source or Header  |  1998-08-02  |  3KB  |  120 lines

  1. /*========================================================================*
  2.  |  File: Dial.c                                       Date: 02 Aug 1998  |
  3.  *------------------------------------------------------------------------*
  4.  |                Use the PCD3311/3312 to produce dial tones              |
  5.  |                                                                        |
  6.  *========================================================================*/
  7.  
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <ctype.h>
  12. #include <time.h>
  13. #include <exec/types.h>
  14. #include <proto/exec.h>
  15. #include <proto/dos.h>
  16. #include <proto/i2c.h>
  17.  
  18. struct Library *I2C_Base;
  19.  
  20.  
  21. #define PCD3312 0x48    /* may be jumpered as 0x4A, too */
  22.  
  23. /* table: recognized dial keys on the command line */
  24. /* (send code 0x10 + offset to hear them) */
  25. char caKeys[] = "0123456789ABCD*#";
  26.  
  27.  
  28.  
  29. void LibClose( struct Library **ppLib )
  30.     {
  31.     if( *ppLib )
  32.         {
  33.         CloseLibrary( *ppLib );
  34.         *ppLib = NULL;
  35.         }
  36.     }
  37.  
  38.  
  39.  
  40. struct Library* LibOpen( STRPTR strName, LONG lVersion )
  41.     {
  42.     struct Library* pLib;
  43.  
  44.     pLib = OpenLibrary( strName, lVersion );
  45.     if( !pLib )
  46.         {
  47.         printf( "Can't open %s", strName );
  48.         printf( lVersion ? " V%ld+\n" : "\n", lVersion );
  49.         exit( 10 );
  50.         }
  51.     else
  52.         return pLib;
  53.     }
  54.  
  55.  
  56.  
  57. void CleanUp()
  58.     {
  59.     SendI2C( PCD3312, 1, "\0" );
  60.     LibClose( &I2C_Base );
  61.     LibClose( (struct Library **)&DOSBase );
  62.     }
  63.  
  64.  
  65.  
  66. int main( int argc, char *argv[] )
  67.     {
  68.     int i;
  69.     long err;
  70.     char *s, *t;
  71.     char c;
  72.  
  73.     if( argc < 2 )
  74.         {
  75.         printf( "Usage: \e[2m%s <keys>\e[0m, ", argv[0] );
  76.         printf( "e.g. %s 01910 *66257# ABCD\n", argv[0] );
  77.         return 5;
  78.         }
  79.  
  80.     DOSBase  = NULL;
  81.     I2C_Base = NULL;
  82.     atexit( CleanUp );          /* close libraries upon Ctrl-C, too */
  83.     I2C_Base = LibOpen( "i2c.library", 39 );
  84.     DOSBase  = (struct DosLibrary *)LibOpen( "dos.library", 0 );
  85.  
  86.     i=1;
  87.     while( TRUE )
  88.         {
  89.         s = argv[ i ];
  90.         while( *s )
  91.             {
  92.             t = strchr( caKeys, toupper( *s ) );
  93.             if( t != NULL )
  94.                 {
  95.                 c = (t - caKeys) + 0x10;
  96.                 SendI2C( PCD3312, 1, &c );
  97.                 printf( "%c", *s );
  98.                 }
  99.             else
  100.                 printf( "?" );
  101.             Delay( 8 );
  102.             err = SendI2C( PCD3312, 1, "\0" );
  103.             if( (err & 0xff)==0 )
  104.                 {
  105.                 printf("\nI²C failure: \e[1m%s\e[0m\n", I2CErrText(err) );
  106.                 exit( 10 );
  107.                 }
  108.             fflush( stdout );   /* force GNU C to check for Ctrl-C :-( */
  109.             s++;
  110.             }
  111.         printf( " " );
  112.         if( ++i == argc )
  113.             break;
  114.         Delay( 25 );
  115.         }
  116.     printf( "\n" );
  117.  
  118.     return 0;
  119.     }
  120.